package com.sgb.kjwl.view.ui.fragment.home;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCollectionSpecialBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.user.BigHomeAndroidEntity;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.home.CentralizedMining4Adapter;
import com.sgb.kjwl.view.ui.adapter.home.CommodityConditionsAdapter;
import com.sgb.kjwl.view.widget.InstantMiningSelectionTypePopWindow;
import com.sgb.kjwl.view.widget.pick.CenterLayoutManager;
import com.sgb.kjwl.viewmodel.home.CollectionSpecialModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


/**
 * Description:
 * Author cfk
 * Time
 * 采集服务中的专项fragment
 */
public class CollectionSpecialFragment extends AppFragment {

    private FragmentCollectionSpecialBinding binding;
    private MainActivity activity;
    public static final String GROUPBUY = "groupbuy";//集采
    @Inject
    CollectionSpecialModel viewModel;
    private CentralizedMining4Adapter centralizedMiningAdapter;
    private String keyWords = "";

    //地区
    private String cityNo;
    //状态
    private String statusNo;
    //时间
    private String timeNo = "";
    //二级分类编号
    private String secondTypeNo;

    private String type;

    private String methodNo;//集采方式

    private String offerNo;//报价类型

    private String groupType;// “1”.即报集采、“2”.汇集采购、“”.集采大厅、“3”.企业专项、

    private String[] titles;
    List<MenuEntity> dictdata;
    ArrayList<MenuEntity> entitielist = new ArrayList<>();

    private int mCentralized;
    private InstantMiningSelectionTypePopWindow instantMiningSelectionTypePopWindow;
    private CommodityConditionsAdapter commodityConditionsAdapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainActivity) {
            activity = (MainActivity) context;
        }
    }
    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_collection_special, container, false);
        return binding.getRoot();

    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }
    public void initView() {
        viewModel.recomandCate(GROUPBUY);
        viewModel.getJiCaiList(type, secondTypeNo, groupType, cityNo, statusNo, timeNo, mCentralized, keyWords, methodNo, offerNo, true);
        if (centralizedMiningAdapter == null) {
            centralizedMiningAdapter = new CentralizedMining4Adapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvOption3.setLayoutManager(layoutManager);
        binding.rlvOption3.setAdapter(centralizedMiningAdapter);
        binding.rvConditionSelection.setLayoutManager(new CenterLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        instantMiningSelectionTypePopWindow = new InstantMiningSelectionTypePopWindow(getActivity());
        binding.editView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == dictdata || dictdata.size() == 0)
                    return;
                instantMiningSelectionTypePopWindow.showPopwindow(binding.editView, 0, 0);
                instantMiningSelectionTypePopWindow.setData(dictdata);
            }
        });
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.getJiCaiList(type, secondTypeNo, groupType, cityNo, statusNo, timeNo, mCentralized, keyWords, methodNo, offerNo, true);
            }
        });
    }
    public void showList(List<CentralizedMiningEntity> list) {
        if (list.size() > 0) {
            centralizedMiningAdapter.setDatas(list);
        } else {
            centralizedMiningAdapter.setDatas(null);
        }
//        centralizedMiningAdapter.setOnItemClick(new AdapterOnItemClick<CentralizedMiningEntity>() {
//            @Override
//            public void onItemClick(CentralizedMiningEntity centralizedMiningEntity, int position) {
//                viewModel.getH5Url(centralizedMiningEntity.getNo());
//            }
//        });
    }
    public void showOption1View(BigHomeAndroidEntity datas) {
        if(datas==null){
            return;
        }
        List<MenuEntity> childList = datas.getTagList().get(0).getChildList();
        Log.i("api123",childList.get(0).getName());
        commodityConditionsAdapter = new CommodityConditionsAdapter(getContext(), childList);
        binding.rvConditionSelection.setAdapter(commodityConditionsAdapter);
        if (null != datas && datas.getTagList().get(0).getChildList().size() > 0) {
            dictdata = datas.getTagList().get(0).getChildList();
            int size = datas.getTagList().get(0).getChildList().size();
            titles = new String[size];
            for (int i = 0; i < size; i++) {
                titles[i] = datas.getTagList().get(0).getChildList().get(i).getName();
            }
        } else {
            MToast.showToast(getActivity(), "数据未加载");
        }
    }
    public void showUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }

}
